package com.anthonybennis.blender.client;

import com.anthonybennis.blender.client.images.ImageClientBundle;
import com.anthonybennis.blender.client.pages.HomePage;
import com.anthonybennis.blender.client.pages.NewsPage;
import com.anthonybennis.blender.client.pages.OnlineWikiPage;
import com.anthonybennis.blender.client.pages.TipOfTheDayPage;
import com.anthonybennis.blender.client.pages.keyboard.ShortcutsPage;
import com.anthonybennis.blender.client.pages.videos.VideoPage;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.widget.button.Button;

/**
 * 
 * @author abennis
 */
public class Navigatgor 
{
	public enum PAGE {HOME, SHORTCUTS, WIKI, VIDEOS, NEWS, TIP_OF_THE_DAY};
	
	private final Panel _pageContainer;
	private final ImageClientBundle _imageClientBundle;
	private Button _backButton;
	
	private Widget _homePageWidget;
	private Widget _shortCutsPageWidget;
	private Widget _onlineWikiWidget;
	private Widget _videoPageWidget;
	private Widget _magazinePageWidget;
	private Widget _newsPageWidget;
	private Widget _tipOfTheDayWidget;
	
	private Widget _currentPage;
	
	
	/**
	 * Constructor.
	 * @param pageContainer
	 */
	public Navigatgor(Panel pageContainer, ImageClientBundle imageClientBundle)
	{
		_pageContainer = pageContainer;
		_imageClientBundle = imageClientBundle;
	}
	
	/**
	 * 
	 * @param page
	 */
	public void open(PAGE page)
	{
		if (_currentPage != null)
		{
			_pageContainer.remove(_currentPage);
		}
		
		this.setHomeButtonVisible(true);
		
		switch (page) 
		{
			case HOME:
			{	
				_currentPage = this.getHomePageWidget();
				this.setHomeButtonVisible(false);
				break;
			}
			case SHORTCUTS:
			{	
				_currentPage = this.getShortCutsPage();
				break;
			}
			case WIKI:
			{	
				_currentPage = this.getOnlineWikiPage();
				break;
			}
			case VIDEOS:
			{	
				_currentPage = this.getVideoPage();
				break;
			}
			case NEWS:
			{	
				_currentPage = this.getNewsPage();
				break;
			}
			case TIP_OF_THE_DAY:
			{
				_currentPage = this.getTipOfTheDay();
				break;
			}
			default:
			{
				_currentPage = this.getShortCutsPage();
				break;
			}
		}
		
		_currentPage.setSize("100%", "100%");
		_pageContainer.add(_currentPage);
		
	}
	
	/**
	 * 
	 * @param backButton
	 */
	protected void setBackButtonWidget(Button backButton)
	{
		_backButton = backButton;
	}
	
	/**
	 * 
	 * @param visible
	 */
	private void setHomeButtonVisible(boolean visible)
	{
		if (_backButton != null)
		{
			_backButton.setVisible(visible);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget getHomePageWidget()
	{	
		if (_homePageWidget == null)
		{
			HomePage homePage = new HomePage(_imageClientBundle, this);
			_homePageWidget = homePage.create();
		}
		
		return _homePageWidget;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget getShortCutsPage()
	{
		if (_shortCutsPageWidget == null)
		{
			ShortcutsPage shortCutsPage = new ShortcutsPage();
			_shortCutsPageWidget = shortCutsPage.create();
		}
		
		return _shortCutsPageWidget;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget getOnlineWikiPage()
	{
		if (_onlineWikiWidget == null)
		{
			OnlineWikiPage onlineWikiPage = new OnlineWikiPage();
			_onlineWikiWidget = onlineWikiPage.create();
		}
		
		return _onlineWikiWidget;
	}
	
	private Widget getVideoPage()
	{
		if (_videoPageWidget == null)
		{
			VideoPage videoPage = new VideoPage();
			_videoPageWidget = videoPage.create();
		}
		
		return _videoPageWidget;
	}
	
	/**
	 * 
	 * @return
	 */
	private Widget getNewsPage()
	{
		if (_newsPageWidget == null)
		{
			NewsPage newsPage = new NewsPage();
			_newsPageWidget = newsPage.create();
		}
		
		return _newsPageWidget;
	}
	
	private Widget getTipOfTheDay()
	{
		if (_tipOfTheDayWidget == null)
		{
			TipOfTheDayPage newsPage = new TipOfTheDayPage();
			_tipOfTheDayWidget = newsPage.create(this);
		}
		
		return _tipOfTheDayWidget;
		
		
	}
}